<?php
/*
*合并lib文件
* $ver:版本号,空则不合并, $dir:目录 $cache_pre合并和的文件名前缀
*/
function require_libs($ver="", $dir="./lib/", $cache_pre="cache_lib_") {
	$cache = $dir.'/'.$cache_pre.$ver.'.php';
	if(file_exists($cache)) {
		require $cache;
		return true;
	}
	
	$libs = array();
	if ($handle = opendir($dir)) {
		while (false !== ($file = readdir($handle))) {
				$pathinfo = explode('.', $file);
				if($pathinfo[count($pathinfo)-1]==='php') {
					$libs[] = $dir.$file;
				}			
		}
	}
	//D($libs);
	if(empty($libs)) return null;
	
	if(empty($ver)) {
		foreach($libs as $v) {
			if(strpos($v, $cache_pre)===false) require $v;
		}
		return true;
	}
	
	$cont = '';
	foreach($libs as $k=>$v) {
		if(strpos($v, $cache_pre)!==false) {
			chmod($v, 0666);
			unlink($v);
			continue;
		}
		$cont .= file_get_contents($v);
	}
	$patterns = array('/\n[\s| ]*\r/',);
	$cont=preg_replace($patterns, '', $cont);
	file_put_contents($cache, $cont);
	require $cache;
}
/*
 *加载js文件
* $ver:版本号,空则不合并, $dir:目录 $cache_pre合并和的文件名前缀
*/
function load_js($ver="", $js=array(), $cache_dir="js_cache/") {
	if(empty($js)) return false;
	//如果没有版本号 则分别加载
	if(empty($ver)) {
		foreach($js as $v) {
			foreach($v as $vv) {
				echo '<script type="text/javascript" src="'.MJS_1.'static/js/'.$vv.'"></script>'.PHP_EOL;
			}
		}
		return true;
	}
	
	$ver = 'version_'.$ver;
	$jsdir = APP.'/html/'.$cache_dir.$ver.'/';
	if(false!==strpos(ROOT, 'http://www.xda.cn'))
			$jspath = 'http://img.xda-china.com/index/html/'.$cache_dir.$ver.'/';
	else $jspath = HTML_PATH.$cache_dir.$ver.'/';

	//如果存在当前版本文件 则直接加载
	if(is_dir($jsdir)) {
		if ($handle = opendir($jsdir)) {
			while (false!== ($file = readdir($handle))) {
				$pathinfo = explode('.', $file);
				if($pathinfo[count($pathinfo)-1]==='js') {
					echo '<script type="text/javascript" src="'.$jspath.$file.'"></script>';
				}
			}
		}
		return true;
	}else{
		if(!is_dir(APP.'/html/'.$cache_dir))	mkdir(APP.'/html/'.$cache_dir, 0777);
		if(!is_dir($jsdir))	mkdir($jsdir, 0777);
	}
	
	$cont = '';
	require_once Bin.'/JSMin.php';
	foreach($js as $k=>$v) {
		foreach($v as $vv) {
			$cont .= file_get_contents(APP.'/static/js/'.$vv);
		}
		$cont = JSMin::minify($cont); //压缩js
		$flag = file_put_contents(HTML_DIR.$cache_dir.$ver.'/'.$k.'.js', $cont);
		if(false!==$flag) echo '<script type="text/javascript" src="'.$jspath.$k.'.js"></script>';//加载
		$cont = '';
	}
	
	//if(false!==$flag) { //如果写入成功
		/*
		if ($handle = opendir(HTML_DIR)) {//删除原来的cache
			while (false!== ($file = readdir($handle))) {
					$pathinfo = explode('.', $file);
					if($pathinfo[count($pathinfo)-1]==='js') {
						if(strpos($file, $cache_pre)!==false && $file!==$cache_pre.$ver.'.js') {
							chmod(HTML_DIR.$file, 0666);
							unlink(HTML_DIR.$file);
						}
					}			
			}
		}
		*/
		
	/*}else{ //没有写入成功则分别加载
		foreach($js as $v) {
			echo '<script type="text/javascript" src="'.MJS_1.'static/js/'.$v.'"></script>'.PHP_EOL;
		}
	}
	*/
	
}

?>